package com.cn.wanxi.util;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class MyFIlter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //        1.控制乱码
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        request.setCharacterEncoding("UTF-8");//页面请求后台，页面的值传递到后台强制将编码转换成UTF-8
        response.setCharacterEncoding("UTF-8");
//        response.setContentType("application/json; charset=utf-8");
        response.setHeader("Access-Control-Allow-Origin", "http://localhost:8088");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        String username = (String) request.getSession().getAttribute("username");
        System.out.println(username);
        String path = request.getServletPath();//得到当前的访问路径
        System.out.println(path);
//        if (path.startsWith("/back/")) {
//
//            if (username != null) {
//                filterChain.doFilter(request, response);//放行
//            } else {
//                response.getWriter().println(400);
//            }
//        } else {
//            filterChain.doFilter(request, response);//放行
//        }
        filterChain.doFilter(request, response);//放行

    }

    @Override
    public void destroy() {

    }
}
